using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using G4KFramework;

namespace G4KFramework.Inputs
{
    public class GGameInput
    {
        Dictionary<string, GInput> Inputs = new Dictionary<string, GInput>();

        public GGameInput()
        {
        }

        public GInput GetInput(string action)
        {
            if (Inputs.ContainsKey(action) == false)
                Inputs.Add(action, new GInput());

            return Inputs[action];
        }

        //----------------------------------------------------------------
        // Kiem tra xem co thuc thi input ung voi action ko
        public bool IsPressed(string action)
        {
            if (!Inputs.ContainsKey(action))
                return false;

            return Inputs[action].IsPressed();
        }

        //----------------------------------------------------------------
        // Them vung can kiem tra action TouchTap
        public void AddTouchTapInput(string action, Rectangle touchArea, bool isReleasedPreviously)
        {
            GetInput(action).AddTouchTapInput(touchArea, isReleasedPreviously);
        }

        //----------------------------------------------------------------
        // Them vung can kiem tra action TouchSlide
        public void AddTouchSlideInput(string action, GInput.Direction direction, float slideDistance)
        {
            GetInput(action).AddTouchSlideInput(direction, slideDistance);
        }

        //----------------------------------------------------------------
        // Them vung can kiem tra action Gesture
        public void AddTouchGestureInput(string action, GestureType gesture, Rectangle rectangle)
        {
            GetInput(action).AddTouchGesture(gesture, rectangle);
        }

        //----------------------------------------------------------------
        // Update GameInput
        public void BeginUpdate()
        {
            GInput.BeginUpdate();
        }
        public void EndUpdate()
        {
            GInput.EndUpdate();
        }

        //----------------------------------------------------------------
        // Cac thong tin ve Gesture
        public Vector2 CurrentGesturePosition(string action)
        {
            return GetInput(action).CurrentGesturePosition();
        }
        public Vector2 CurrentGesturePosition2(string action)
        {
            return GetInput(action).CurrentGesturePosition2();
        }
        public Vector2 CurrentGestureDelta(string action)
        {
            return GetInput(action).CurrentGestureDelta();
        }
        public Vector2 CurrentGestureDelta2(string action)
        {
            return GetInput(action).CurrentGestureDelta2();
        }

        //----------------------------------------------------------------
        // Vi tri touch theo pixel
        public Point CurrenTouchPoint(string action)
        {
            Vector2? currentPosition = GetInput(action).CurrentTouchPosition();
            if (currentPosition == null)
                return new Point(-1, -1);

            return new Point((int)currentPosition.Value.X, (int)currentPosition.Value.Y);
        }

        //----------------------------------------------------------------
        // Vi tri touch theo toa do
        public Vector2 CurrenTouchPosition(string action)
        {
            Vector2? currentPosition = GetInput(action).CurrentTouchPosition();
            if (currentPosition == null)
                return new Vector2(-1, -1);

            return (Vector2)currentPosition;
        }

        //----------------------------------------------------------------
        // Tra ve gia tri float la do scale cua thao tac pinch
        public float CurrentGestureScaleChange(string action)
        {
            if (!GetInput(action).PinchGestureAvaible) return 0;

            Vector2 currentPositionFingerOne = CurrentGesturePosition(action);
            Vector2 previousPositionFingerOne = currentPositionFingerOne - CurrentGestureDelta(action);

            Vector2 currentPositionFingerTwo = CurrentGesturePosition2(action);
            Vector2 previousPositionFingerTwo = currentPositionFingerTwo - CurrentGestureDelta2(action);

            float currentDistance = Vector2.Distance(currentPositionFingerOne, currentPositionFingerTwo);
            float previousDistance = Vector2.Distance(previousPositionFingerOne, previousPositionFingerTwo);

            float scaleChange = (currentDistance - previousDistance) * .01f;
            return scaleChange;
        }
    }
}
